/*
角色行动结束奖励结算
*/
window._MCRD = {


    initXwObj(level){

        //var per = (level%1000)/100*0.9+0.1;
        var per = 1;
        var once = Math.floor(level/10)+1;
        return {
                        per:per,
                        once:once,
                        value:0
                    };

    },


    getDropQua:function(){

        var randomnmb = Math.random();
        var defaultqua = 'D';
        for(var key in _CS.qua){
            if(randomnmb<_CS.qua[key].per){
                return key;
            }
        }

        return defaultqua;
    },


    //根据关卡和层数构建修为奖励
    getXwObjByChapter:function(){

        var globalxw = _MXLU.getXwByChapter(window.fightData.chapter);
        var level = window.fightData.level;
        var xwObj = _MCRD.initXwObj(level);

        if(globalxw){
            xwObj.xw = globalxw.xw;
            xwObj.max = globalxw.lv*100;
            if(xwObj.max>_CS.maxxw){
                xwObj.max = _CS.maxxw;
            }
        }

        return xwObj;

    },


    setReward:function(player,reward){

        //if(!player.isplayer){
        //    return ;
        //}

        if(
            player.isZhaohuan
            &&player.sourcePlayer
            &&player.sourcePlayer.hp>0
        ){
            player = player.sourcePlayer;
            reward.player = player;
        }

        if(!player.reward){
            player.reward=[];
        }

        player.reward.push(reward);
    },

    getRewardArr:function(){

        var rewardArr = [];
        for(var key in _CHESSFIGHT.playerNodeMap){

            var player = _CHESSFIGHT.playerNodeMap[key].info;
            var playerRewardArr = _MCRD.getRewardArrOfPlayer(player);
            if(playerRewardArr&&playerRewardArr.length>0){

                rewardArr.push({player:player,arr:playerRewardArr});

            }

        }

        if(rewardArr.length>0){

            _CHESSFIGHT.doGetReward(rewardArr);


            return true;
        }else{
            return false;
        }

    },

    getExp:function(playerLv,armylv,targetNpcType,hardworkPer){

        var exp = 50;

        if(armylv>playerLv){
            exp = Math.floor(exp*(1+(armylv-playerLv)/50));
        }else if(playerLv-armylv<50){
            exp = Math.floor(exp*(1-(playerLv-armylv)/50));
        }
        if(exp<1){
            exp=1;
        }else if(exp>100){
            exp=100;
        }

        var npcConfig = _NPC[targetNpcType];

        if(npcConfig
            &&npcConfig.qua
            &&_CS.qua[npcConfig.qua]
            &&_CS.qua[npcConfig.qua].expper
        ){
            exp=exp*_CS.qua[npcConfig.qua].expper;
        }
        if(hardworkPer){
            exp+=hardworkPer;
        }

        return exp;
    },

    /*
    getExp:function(playerLv,armylv,targetNpcType,hardworkPer){

        var exp = 100*(1000-playerLv)/1000+10;

        var npcConfig = _NPC[targetNpcType];

        if(armylv>playerLv){
            exp = ((armylv-playerLv)/10+1)*exp;
        }
        if(npcConfig
            &&npcConfig.qua
            &&_CS.qua[npcConfig.qua]
            &&_CS.qua[npcConfig.qua].expper
        ){
            exp=exp*_CS.qua[npcConfig.qua].expper;
        }
        if(hardworkPer){
            exp=Math.floor(exp*hardworkPer);
        }

        return exp;
    },
    */

    getRewardArrOfPlayer:function(player){

        var rewardArr = [];

        if(!player){
            return rewardArr;
        }

        var playerData = _ROLEDATA.getRoleDataOfPlayer(player);
        var yuanhun = false;

        if(
            player.isplayer
            &&!player.isZhaohuan
            &&player.reward
            &&player.hp>0
            &&player.reward.length>0
        ){
            
            //var maxitemDropLv = _SHOP['丹药商'].sell.length-1;
            //var maxfitemDropLv = _SHOP['道具商'].sell.length-1;
            var gold = 0;
            var exp = 0;
            var playerLv = playerData.lv;
            var hardworkPer = playerData.hardwork;
            //var itemDropPer = playerData.itemDrop/100;
            var equipDropPer = playerData.equipDrop/100;
            var skillDropPer = playerData.skillDrop/100;
            //var fitemDropPer = playerData.fitemDrop/100;
            //var itemDropLv = playerData.dropLv>maxitemDropLv?maxitemDropLv:playerData.dropLv;
            //var fitemDropLv = playerData.dropLv>maxfitemDropLv?maxfitemDropLv:playerData.dropLv;;
            var shareExpPer = playerData.shareExp/100;
            var shareXwPer = playerData.shareXw/100;
            var shareArr = (shareExpPer||shareXwPer)?_MCRD.getShareArr(player):null;
            //var playerFtype = player.ftype;
            //var playerXw = _CROF.ftypexw[playerFtype];
            //var playerXwNmb = 0;
            var equipArr=[];
            var skillArr=[];
            //var itemArr = _SHOP['丹药商'].sell[itemDropLv];
            //var fightItemArr = _SHOP['道具商'].sell[fitemDropLv];

            //计算任务
            var taskObj = {};

            for(var i=0;i<player.reward.length;i++){
                var target = player.reward[i].target;
                var targetname = player.reward[i].target.name;
                var targetNpcType = player.reward[i].target.npctype;
                var armyData = _ROLEDATA.getRoleDataOfPlayer(target);
                var armylv = armyData.lv;

                exp+=_MCRD.getExp(playerLv,armylv,targetNpcType,hardworkPer);

                gold+=armylv*100;

                if(taskObj[targetNpcType]){
                    taskObj[targetNpcType]+=1;
                }else{
                    taskObj[targetNpcType]=1;
                }

                var dropSkill = _SKILLHEPLER.getSkillOfDrop(targetname);
                    if(dropSkill&&dropSkill.length>0){
                        for(var j=0;j<dropSkill.length;j++){
                            var skillname = dropSkill[j];
                            if(_SKILLHEPLER.isSkillCanDrop(skillname)){
                                skillArr.push(skillname);
                            }
                        }
                }

                for(var j=0;j<target.equip.length;j++){
                    equipArr.push(target.equip[j].type);
                }

                if(armyData.yuanhun){
                    yuanhun = true;
                }

            }

            if(gold>0){
                _GAMEDATAHELPER.oprMoney(gold);
                rewardArr.push({face:player.face,name:player.name,msg:"钱+"+gold});
            }

            if(equipArr.length>0&&Math.random()<(_CS.baseDropPer+equipDropPer)){
                var temprandomEquip = _CU.randomObj(equipArr);
                var dropEquip = _IOF.createEquip(temprandomEquip,{qua:_MCRD.getDropQua()});
                _MCFC.initEquipXw(dropEquip);
                _GAMEDATAHELPER.pushItem(dropEquip);
                rewardArr.push({face:player.face,name:player.name,msg:temprandomEquip+"+1"});
            }

            if(skillArr.length>0&&Math.random()<(_CS.baseDropPer+skillDropPer)){
                var temprandomskill = _CU.randomObj(skillArr);
                _GAMEDATAHELPER.pushItem(_IOF.createObjByName(temprandomskill,null));
                rewardArr.push({face:player.face,name:player.name,msg:temprandomskill+"+1"});
            }

            /*
            //丹药掉落
            if(itemArr&&itemArr.length>0&&Math.random()<(_CS.baseDropPer+itemDropPer)){
                var tempItem = _CU.randomObj(itemArr);
                _GAMEDATAHELPER.pushItem(_IOF.createObjByName(tempItem,null));
                rewardArr.push({face:player.face,name:player.name,msg:tempItem+"+1"});
            }

            //战斗道具掉落
            if(fightItemArr&&fightItemArr.length>0&&Math.random()<(_CS.baseDropPer+fitemDropPer)){
                var tempItem = _CU.randomObj(fightItemArr);
                _GAMEDATAHELPER.pushItem(_IOF.createObjByName(tempItem,null));
                rewardArr.push({face:player.face,name:player.name,msg:tempItem+"+1"});
            }
            */

            //获得经验
            if(exp>0){
                _MCRD.getRewardExp(player,playerLv,exp,rewardArr);
                if(shareExpPer>0&&shareArr&&shareArr.length>0){
                    var shareExpValue = Math.floor(exp*shareExpPer);
                    for(var i=0;i<shareArr.length;i++){
                        _MCRD.getRewardExp(shareArr[i],null,shareExpValue,rewardArr);
                    }
                }
            }


            //结算任务
            //for(var key in taskObj){
            //    _MXYT.doTask(key,taskObj[key]);
            //}
        }

        //计算杀意和冤魂  此计算不分是否玩家
        if(player.reward&&player.reward.length>0){
            if(playerData.sblood){
                player.eg+=playerData.sblood;
                rewardArr.push({face:player.face,name:player.name,msg:"杀意"+playerData.sblood});
            }
            if(player.isplayer){
                if(yuanhun&&!playerData.strongEnergy){
                    player.eg=0;
                    rewardArr.push({face:player.face,name:player.name,msg:"怨魂"});
                }
            }else{
                for(var i=0;i<player.reward.length;i++){
                    var target = player.reward[i].target;
                    var armyData = _ROLEDATA.getRoleDataOfPlayer(target);
                    if(armyData.yuanhun){
                        player.eg=0;
                        rewardArr.push({face:player.face,name:player.name,msg:"怨魂"});
                        break;
                    }
                }
            }
        }

        player.reward = null;

        return rewardArr;

    },

    //获取共享经验数组
    getShareArr:function(player){

        var arr = [];

        for(var key in _CHESSFIGHT.playerNodeMap){

            var playerInfo = _CHESSFIGHT.playerNodeMap[key].info;

            if(playerInfo.isplayer
                &&!playerInfo.isZhaohuan
                &&playerInfo.code!=player.code){

                arr.push(playerInfo);

            }

        }

        return arr;
    },

    //每次升级增加修为
    getRewardXwByLv:function(player,addlv){

        var npcxwConfig = _ROLEDATA.getRoleXwLevel(player);

        var godson = _ROLEDATA.getRealAttr(player,'godson');

        if(npcxwConfig){
            if(player.xw){
                for(var key in player.xw){
                    if(player.xw[key]<_CS.maxxw||godson){
                        var xwlevel = npcxwConfig[key];
                        if(window._XWLEVEL[xwlevel]){
                            var min = window._XWLEVEL[xwlevel][0];
                            var max = window._XWLEVEL[xwlevel][1];
                            player.xw[key]+=_CU.randomXwValue(min,max,addlv);
                            if(player.xw[key]>_CS.maxxw&&!godson){
                                player.xw[key]=_CS.maxxw;
                            }
                        }
                    }
                }
            }
        }
    },

    //增加经验
    getRewardExp:function(player,playerLv,exp,rewardArr){

        var maxlv = _EXPMANAGER.getMaxLevel();
        var maxExp = _EXPMANAGER.getExpByLevel(maxlv);

        if(!playerLv){
            playerLv = _ROLEDATA.getRoleDataOfPlayer(player).lv;
        }

        if(player.exp+exp>maxExp){
            exp = maxExp-player.exp;
            player.exp=maxExp;
        }else{
            player.exp+=exp;
        }
        var newplayerData = _ROLEDATA.getRoleDataOfPlayer(player);
        if(playerLv&&newplayerData.lv>playerLv){
            var addlv = newplayerData.lv-playerLv;
            if(rewardArr){
                rewardArr.push({player:player,levelup:1,face:player.face,name:player.name,msg:"经验+"+exp});
                rewardArr.push({player:player,levelup:1,face:player.face,name:player.name,msg:"等级+"+addlv});
            }
            
            _MCRD.getRewardXwByLv(player,addlv);
            

        }else{
            if(rewardArr){
                rewardArr.push({player:player,face:player.face,name:player.name,msg:"经验+"+exp});
            }
        }
    },

    //实际获得修为
    getRewardXw:function(player,xwObj,rewardArr){

        var godson = _ROLEDATA.getRealAttr(player,'godson');

        if(
            player.xw
            &&player.xw[xwObj.xw]
            &&(
                (//玩家修为小于上限
                    player.xw[xwObj.xw]<xwObj.max
                )||(//玩家修为超过上限且全局修为已满且为神子
                    xwObj.max>=999
                    &&godson
                    &&player.xw[xwObj.xw]<9999
                )
            )
        ){
            var xwMessage =_XW[xwObj.xw].label+"+"+xwObj.value+" ";
            player.xw[xwObj.xw]+=xwObj.value;

            var realmax = godson?9999:xwObj.max;

            if(player.xw[xwObj.xw]>realmax){
                player.xw[xwObj.xw]=realmax;
            }
            
            if(xwMessage.length>0){
                rewardArr.push({player:player,face:player.face,name:player.name,msg:xwMessage});
            }
        }
    },

    createRewardTalk:function(rewardArr){
        var levelup = false;
        var levelUpPlayer = [];
        if(rewardArr&&rewardArr.length>0){
            for(var i=0;i<rewardArr.length;i++){
                if(rewardArr[i].levelup){
                    levelup=true;
                    levelUpPlayer.push(rewardArr[i].player);
                }
            }
        }
        return {
            talk:rewardArr,
            talkType:'chessFightReward',
            levelup:true,
            levelUpPlayer:levelUpPlayer
        }
    },


}